//
//  DetailTableViewCell.m
//  XiMaLaYa
//
//  Created by dllo on 15/11/14.
//  Copyright © 2015年 L-G. All rights reserved.
//

#import "DetailTableViewCell.h"
#import <UIImageView+WebCache.h>
#import <SDWebImageManager.h>

#define HEIGHT  [[UIScreen mainScreen]bounds].size.height / 667
#define WIDTH   [[UIScreen mainScreen]bounds].size.width / 375

@implementation DetailTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.labelTitle = [[UILabel alloc] init];
        self.labelTitle.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:self.labelTitle];
        [_labelTitle release];
        
        self.labelNickName = [[UILabel alloc] init];
        self.labelNickName.textColor = [UIColor grayColor];
        self.labelNickName.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.labelNickName];
        [_labelNickName release];
        
        self.labelPlaysCounts = [[UILabel alloc] init];
        self.labelPlaysCounts.font = [UIFont systemFontOfSize:13];
        self.labelPlaysCounts.textColor = [UIColor grayColor];
        [self.contentView addSubview:self.labelPlaysCounts];
        [_labelPlaysCounts release];
        
        self.labelTracks = [[UILabel alloc] init];
        self.labelTracks.font = [UIFont systemFontOfSize:13];
        self.labelTracks.textColor = [UIColor grayColor];
        [self.contentView addSubview:self.labelTracks];
        [_labelTracks release];
        
        self.imageCover = [[UIImageView alloc] init];
        [self.contentView addSubview:self.imageCover];
        [_imageCover release];
        
        self.imagePlaysCounts = [[UIImageView alloc] init];
        self.imagePlaysCounts.image = [UIImage imageNamed:@"videoplay"];
        [self.contentView addSubview:self.imagePlaysCounts];
        [_imagePlaysCounts release];
        
        self.imageTracks = [[UIImageView alloc] init];
        self.imageTracks.image = [UIImage imageNamed:@"album_tracks@2x"];
        [self.contentView addSubview:self.imageTracks];
        [_imageTracks release];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.imageCover.frame = CGRectMake(12 * WIDTH, 10 * HEIGHT, 95 * WIDTH, 95 * HEIGHT);
    self.labelTitle.frame = CGRectMake(115 * WIDTH, 15 * HEIGHT, self.frame.size.width / 3 * 2  - 30 * WIDTH, 20 * HEIGHT);
    self.imagePlaysCounts.frame = CGRectMake(115 * WIDTH, 82 * HEIGHT, 15 * WIDTH, 15 * WIDTH);
    self.labelNickName.frame = CGRectMake(135 * WIDTH, 45 * HEIGHT, self.frame.size.width / 3 * 2, 20 * HEIGHT);
    self.labelPlaysCounts.frame = CGRectMake(135 * WIDTH, 82 * HEIGHT, 100 * WIDTH, 15 * HEIGHT);
    self.imageTracks.frame = CGRectMake(215 * WIDTH, 80 * HEIGHT, 18 * HEIGHT, 18 * HEIGHT);
    self.labelTracks.frame = CGRectMake(235 * WIDTH, 82 * HEIGHT, 50 * WIDTH, 15 * HEIGHT);
}

- (void)setInforDetail:(InforDetail *)inforDetail
{
    if (_inforDetail != inforDetail) {
        [_inforDetail release];
        _inforDetail = [inforDetail retain];
    }
    self.labelTitle.text = inforDetail.title;
    
    self.labelNickName.text = inforDetail.nickname;
    
    NSURL *url = [NSURL URLWithString:inforDetail.coverMiddle];
    
    [self.imageCover sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
    
    if ([inforDetail.playsCounts floatValue] > 10000) {
        self.labelPlaysCounts.text = [NSString stringWithFormat:@"%0.2f万",[inforDetail.playsCounts floatValue] / 10000];
    }else
    {
        self.labelPlaysCounts.text = inforDetail.playsCounts.description;
    }
    
    self.labelTracks.text = [NSString stringWithFormat:@"%ld集",[inforDetail.tracks integerValue]];
}
- (void)setSearchModel:(SearchModel *)searchModel
{
    if (_searchModel != searchModel) {
        [_searchModel release];
        _searchModel = [searchModel retain];
    }
    self.labelTitle.text = searchModel.title;
    
    self.labelNickName.text = searchModel.nickname;
    
    NSURL *url = [NSURL URLWithString:searchModel.cover_path];
    [self.imageCover sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
    
    if ([searchModel.play floatValue] > 10000) {
        self.labelPlaysCounts.text = [NSString stringWithFormat:@"%0.2f万",[searchModel.play floatValue] / 10000];
    }else
    {
        self.labelPlaysCounts.text = searchModel.play.description;
    }
    
    self.labelTracks.text = [NSString stringWithFormat:@"%ld集",[searchModel.tracks integerValue]];
}

+ (DetailTableViewCell *)detailTableViewCellWith:(UITableView *)tableView
{
    static NSString *cellIdentify = @"cellIdentify";
    DetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (!cell) {
        cell = [[DetailTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentify];
    }
    return cell;
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
